export const others = {
	methods: {
		//富文本的回调
		navigate(e) {
			if (e.href && e.href.indexOf('http') == -1) { //不完整的链接						
				//文章/问题/标签/专家详情
				let res = e.href.match(new RegExp("(question|article|tag|u)|(\\d+)", 'g'));
				if (res.length == 2) {
					uni.navigateTo({
						url:`/pages/community/${res[0] == 'u' ? 'experts' : res[0]}/detail?id=${res[1]}`
					})
					return;
				}
				res = e.href.match(new RegExp("zone|((?<=zone\/).*?(?=\.htm))", 'g'));
				if (res.length == 2) {
					uni.navigateTo({
						url:`/pages/community/zone/detail?diyname=${res[1]}`
					})
					return;
				}
				// #ifdef MP
				this.$util.uniCopy({
					content: this.vuex_config.upload.cdnurl + e.href,
					success: () => {
						this.$u.toast('链接已复制,请在浏览器中打开')
					}
				})
				// #endif
				// #ifndef MP				
				window.open(this.vuex_config.upload.cdnurl + e.href);
				// #endif
			}
		},
		moreClick() {
			this.$set(this.moreList[1], 'text', this.detail.collected ? '已收藏' : '收藏');
			this.is_more = true;
		},
		cancel() {
			this.is_more = false;
		}
	}
}

// 收藏
export const collection = {
	methods: {
		//收藏
		goCollectionCreate: async function(item, type) {
			let res = await this.$api.goCollectionCreate({
				id: item.id,
				type: type
			});
			this.$u.toast(res.msg);
			if (!res.code) {
				return;
			}
			if (type == 'article' || type == 'question' || type == 'answer') {
				item.collected = true;
			}
		},
		//取消收藏
		goCollectionDelete: async function(item, type) {
			let res = await this.$api.goCollectionDelete({
				id: item.id,
				type: type
			});
			this.$u.toast(res.msg);
			if (!res.code) {
				return;
			}
			if (type == 'article' || type == 'question' || type == 'answer') {
				item.collected = false;
			}
		},
	},
}
// 点赞
export const vote = {
	methods: {
		goVoteCreate: async function(item, type, value = 'up') {
			let res = await this.$api.goVoteCreate({
				id: item.id,
				type: type,
				value: value
			});
			this.$u.toast(res.msg);
			if (!res.code) {
				return;
			}
			item.vote = value;
			item.voteup = res.data.voteup;

		},
		goVoteDelete: async function(item, type, value = 'up') {
			let res = await this.$api.goVoteDelete({
				id: item.id,
				type: type,
				value: value
			})
			this.$u.toast(res.msg);
			if (!res.code) {
				return;
			}
			item.voteup = res.data.voteup;
			item.vote = '';
		},
	}
}
// 关注
export const attention = {
	methods: {
		goAttentionCreate: async function(item, type, callback) {
			let res = await this.$api.goAttentionCreate({
				id: item.id,
				type: type
			})
			this.$u.toast(res.msg);
			if (!res.code) {
				return;
			}
			item.followed = true;
			if (typeof callback == 'function') {
				callback();
			};
		},
		goAttentionDelete: async function(item, type, callback = () => {}) {
			let res = await this.$api.goAttentionDelete({
				id: item.id,
				type: type
			})
			this.$u.toast(res.msg);
			if (!res.code) {
				return;
			}
			item.followed = false;
			if (typeof callback == 'function') {
				callback();
			};
		},
	}
}
//轮播图
export const swipers = {
	methods: {
		//轮播图事件
		clickSwiper(index) {
			let path = this.vuex_config.blockList[index].url;
			if (path == '/' || !path) {
				return;
			}
			if (path.indexOf('http') != -1) {
				this.$u.vuex('vuex_web', this.vuex_config.blockList[index]);
				this.$wanlshop.to('/pages/community/webview/webview');
				return;
			}
			if (path.substr(0, 1) == 'p') {
				path = '/' + path;
			}
			this.$u.route({
				url: path
			})
		},
	}
}
//样式
export const styleRest = {
	methods: {
		getStyle(string) {
			let underline = {};
			let style = {};
			let s = string.split('|');
			s.forEach(item => {
				if (item == 'b') {
					style.fontWeight = 'bold';
				}
				if (item == 'u') {
					underline.textDecoration = 'underline';
				}
				if (item.indexOf('#') != -1) {
					let color = item.split(',');
					style.backgroundImage = `linear-gradient(90deg,${color[0]} 0%, ${color[1]?color[1]:color[0]} 100%)`;
					style.textFillColor = 'transparent'
				}
			})
			return [underline, style];
		},
		setStyle(item) {
			if (item.style) {
				let result = this.getStyle(item.style);
				item.styleArray = result[1];
				item.underline = result[0];
			} else {
				item.styleArray = {};
				item.underline = {};
			}
		}
	}
}
//跳转标签
export const tagClick = {
	methods: {
		navigateTag(id, type) {
			this.$u.route(`/pages/community/tag/detail?id=${id}&type=${type}`)
		}
	}
}
//登录成功之后
export const login = {
	methods: {
		success: async function(id, index = 1) {
			let res = await this.$api.getUserIndex({
				id: id
			});
			if (!res.code) {
				this.$u.toast(res.msg);
				return;
			}
			this.$u.vuex('vuex_user', res.data.user);
			this.$u.vuex('vuex_isAdmin', res.data.isAdmin);
		},
		// #ifdef H5
		async goToAuth() {
			let url = '';
			if(window.location.hash != ''){
				url = window.location.origin + window.location.pathname+'?hashpath=/pages/login/auth'
			}else{
				url = window.location.origin + window.location.pathname.replace(/pages.*/,'pages/login/auth');
			};			
			let res = await this.$api.getAuthUrl({
				platform: 'wechat',
				url: url
			});
			if (!res.code) {
				this.$u.toast(res.msg);
				return;
			}
			window.location.href = res.data;
		},
		// #endif
		// #ifdef APP-PLUS
		goAppLogin(index=1){
			let that = this;
			var all, Service;
			// 1.发送请求获取code
			plus.oauth.getServices(
				function(Services) {
					all = Services;
					Object.keys(all).some(key => {
						if (all[key].id == 'weixin') {
							Service = all[key];
						}
					});
					Service.authorize(
						async function(e) {
							let res = await that.$api.goAppLogin({ code: e.code, scope: e.scope });
							if (!res.code) {
								that.$u.toast(res.msg);
								return;
							}
							if (res.data.user) {
								that.$u.vuex('vuex_token', res.data.user.token);
								that.success(res.data.user.id,index);
								return;
							}
							that.$u.vuex('vuex_third', res.data.third);
							uni.navigateTo({
								url:`/pages/community/login/register?bind=bind`
							})
						},
						function(e) {
							that.$u.toast('授权失败！');
						}
					);
				},
				function(err) {
					that.$u.toast('授权失败！');
				}
			);
		}
	// #endif
	}
}
